package com.okoj.excel_lib_rary.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {



    private static final String[] strHex = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public Md5Util() {
    }

    public static String getFileMD5(String path) {
        BigInteger bi = null;

        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            File file = new File(path);
            FileInputStream fis = new FileInputStream(file);
            while((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }

            fis.close();
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (NoSuchAlgorithmException var8) {
            var8.printStackTrace();
        } catch (IOException var9) {
            var9.printStackTrace();
        }

        return bi.toString(16);
    }

    public static String getMd5OfString(String content) {
        return getMd5OfBytes(content.getBytes());
    }

    public static String getMd5OfBytes(byte[] datas) {
        StringBuffer sb = new StringBuffer();
        if (datas != null && datas.length != 0) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] b = md.digest(datas);

                for(int i = 0; i < b.length; ++i) {
                    int d = b[i];
                    if (d < 0) {
                        d += 256;
                    }

                    int d1 = d / 16;
                    int d2 = d % 16;
                    sb.append(strHex[d1] + strHex[d2]);
                }
            } catch (NoSuchAlgorithmException var8) {
                var8.printStackTrace();
            }

            return sb.toString();
        } else {
            return sb.toString();
        }
    }

}
